package com.baikeyang.common.util.shiro;

import com.baikeyang.common.model.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.ExpiredCredentialsException;

/**
 * Created by lenovo on 2019/5/7.
 */
public class ShiroUtil {

    /**
     * 获取当前登录用户
     * @return
     * @throws ExpiredCredentialsException
     */
    public static User getCurrentUser() throws ExpiredCredentialsException {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if(null != user){
            return user;
        }
        throw new ExpiredCredentialsException("用户已过期，请重新登录");
    }

    public static Long getCurrentUserId () throws ExpiredCredentialsException {
        User user = getCurrentUser();
        return user.getId();
    }

}
